;(function (window, document, $) {
	$.zy = {};

	$.zy.zyObj = {
        baseUrl: ''
	};
    
    $.zy.alertMessage = (function(type, string){
        var classNames = {
            success:    'alert-success',
            info:       'alert-info',
            warning:    'alert-warning',
            danger:     'alert-danger'
        };
        
        return $('<div>').addClass('alert '+classNames[type]).html(string);
    });
    
    $.zy._base = function(){
        $.zy.lazyload();
        $.zy.questionSubmit();
    };
    
    $.zy.loading = (function(){
        return $('<img id="loading" src="'+$.zy.zyObj.baseUrl+'/images/common/loading/horizontal.gif" alt="loading"/>');
    }());

    
    $.zy.bindMoreButtonEven = function(moreButton){
		//when user roll the page and hit the bottom then auto fire the ajax to get more content.
		var opts = $.zy.lazyload.getOpt();
		moreButton.waypoint('remove');
		moreButton.waypoint(function(event, direction){
			moreButton.waypoint('remove');
			$.zy.getMoreContent({
				elem:moreButton,
				success:function(){
					moreButton.waypoint(opts);
				}
			});
		}, opts);
	};
    
    $.zy.lazyload = (function(){
        var loading = $.zy.loading;
        var wrap = $('.lazyload_placeholder');
        var listWrap = $('.content_list');
        var infoMes = $.zy.alertMessage('info', '到底了.');
        listWrap.scrollPagination({
            'contentPage': $.zy.zyObj.baseUrl+'content/more',
//            'contentData': {limit:5},
            'scrollTarget': $(window), // who gonna scroll? in this example, the full window
            'heightOffset': 10, // it gonna request when scroll is 10 pixels before the page ends
            'beforeLoad': function(){ // before load function, you can display a preloader div
                if( ! $('#loading').length)
                    wrap.html(loading.hide());
                
                if( ! this.locked)
                    loading.fadeIn();
            },
            'afterLoad': function(json){ // after loading content, you can use this function to animate your new elements
                loading.fadeOut();
                if(json && json.result && !! json.html)
                {
                    $(json.html).appendTo(listWrap);
                    this.contentPage = json.moreUrl;
                    listWrap.attr('moreUrl', json.moreUrl);
                }
                else
                {
                    this.locked = true;
                    infoMes.appendTo(wrap);
                };
            }
        });
    });
    
    $.zy.questionSubmit = (function(){
        $(document).on('submit', '.submit_form', function(){
            var self = this;
            var formData = $(this).serializeObject();
            var message = $(this).find('.message');
            
            if( ! formData.Content.choiceIds)
            {
                message.html($.zy.alertMessage('warning', '请选择.'));
                return false;
            };
            
            if( ! $(this).attr('locked'))
            {
                $(this).attr('locked', 'locked');
                $.ajax({
                    type: 'post',
                    dataType: 'json',
                    data: formData,
                    url: $.zy.zyObj.baseUrl+'content/submitQuestion',
                    success: function(json){
                        $(self).removeAttr('locked');
                        if(json && json.result)
                        {
                            message.html($.zy.alertMessage('success', '回答正确.'));
                        }
                        else
                        {
                            message.html($.zy.alertMessage('danger', '回答错误.'));
                            var explain = $(self).find('.explain');
                            explain.html('详解: '+json.html);
                            explain.slideDown('normal');
                            return false;
                        };
                    },
                    error: function(){
                        $(self).removeAttr('locked');
                    }
                });
            };
            return false;
        });
    });
	
})(window, document, jQuery);
